using System;
using System.Xml;
using System.Text.RegularExpressions;

namespace UNLV.IAP.WebControls
{
	/// <summary>
	/// Represents a Condition in a SqlWhereBuilder web control.
	/// </summary>
	[Serializable]
	public class SqlWhereBuilderCondition
	{

		private string _fieldId = "";
		private string _opId = "";
		private SqlWhereBuilderValueCollection _values = new SqlWhereBuilderValueCollection();
		private string _andOr = "";

		/// <summary>
		/// The string identifier for the SqlWhereBuilderField used in this condition
		/// </summary>
		public string FieldId
		{
			get {return _fieldId;}
			set {_fieldId = value;}
		}


		/// <summary>
		/// The string identifier for the SqlWhereBuilderOperator used in this condition
		/// </summary>
		public string OperatorId
		{
			get {return _opId;}
			set {_opId = value;}
		}

		/// <summary>
		/// The collection of SqlWhereBuilderValue objects used in this condition
		/// </summary>
		public SqlWhereBuilderValueCollection Values
		{
			get {return _values;}
		}

		/// <summary>
		/// The SQL conjunction (AND or OR) used to connect this condition to the one that precedes it in a compiled WHERE clause.
		/// </summary>
		public string AndOr
		{
			get {return _andOr;}
			set {_andOr = value;}
		}

				
		/// <summary>
		/// Populates this Condition taking property values from an XmlNode
		/// </summary>
		/// <remarks>
		/// The XmlNode passed in should be in the following syntax:
		/// <code>
		/// <![CDATA[
		/// <condition field=xxx operator=xxx andOr=xxx>
		///    <values>...</values>
		/// </condition>
		/// ]]>
		/// </code>
		/// Each value within the &lt;values&gt; tag is represented as follows:
		/// <code>
		/// <![CDATA[
		///		<value name='xxx' value='yyy' friendlyValue='zzz' />
		/// ]]>
		/// </code>
		/// where the <i>name</i> attribute identifies a form input <i>id</i>
		/// found in the appropriate ValueEntry area.
		/// </remarks>
		/// <param name="n">the XmlNode to parse</param>
		public void DeserializeFromNode(XmlNode n)
		{
			// <condition field=xxx operator=xxx andOr=xxx>
			//   <values>...</values>
			// </condition>

			if (n.Attributes["field"] == null) this.FieldId = ""; this.FieldId = n.Attributes["field"].Value;
			if (n.Attributes["operator"] == null) this.OperatorId = ""; this.OperatorId = n.Attributes["operator"].Value;
			if (n.Attributes["andOr"] == null) this.AndOr = ""; this.AndOr = n.Attributes["andOr"].Value;

			XmlNode values = n.SelectSingleNode("values");
			if (values == null)
				this.Values.Clear();
			else
				this.Values.DeserializeFromNode(values);


		}

		/// <summary>
		/// Initializes a SqlWhereBuilderCondition object
		/// </summary>
		public SqlWhereBuilderCondition()
		{
		}

		/// <summary>
		/// Initializes a SqlWhereBuilderCondition object, given string parameters
		/// </summary>
		/// <param name="fieldId">the string <i>id</i> of the SqlWhereBuilderField object to reference in this condition</param>
		/// <param name="opId">the string <i>id</i> of the SqlWhereBuilderOperator object to reference in this condition</param>
		/// <param name="andOr">either "AND", "OR", or ""</param>
		public SqlWhereBuilderCondition(string fieldId, string opId, string andOr)
		{
			this.FieldId = fieldId;
			this.OperatorId = opId;
			this.AndOr = andOr;
		}
	
		/// <summary>
		/// Initializes a SqlWhereBuilderCondition object, with property
		/// values supplied through the given XmlNode
		/// </summary>
		/// <param name="n">the XmlNode to parse</param>
		public SqlWhereBuilderCondition(XmlNode n)
		{
			this.DeserializeFromNode(n);
		}

	
		/// <summary>
		/// Returns TRUE if the given object is equal to the SqlWhereBuilderCondition.
		/// </summary>
		/// <param name="obj"></param>
		/// <returns></returns>
		public override bool Equals(object obj)
		{
			bool retValue = true;

			SqlWhereBuilderCondition v = obj as SqlWhereBuilderCondition;
			if (!(object.ReferenceEquals(v, null)))
				retValue = (
					(string.Compare(v.FieldId, this.FieldId, true) == 0)
					&& (string.Compare(v.OperatorId, this.OperatorId, true) == 0)
					&& (string.Compare(v.AndOr, this.AndOr, true) == 0)
					&& (v.Values.Equals(this.Values))
					);
			else
				retValue = false;

			return retValue;
		}


		public override int GetHashCode()
		{
			return this.ToString().GetHashCode();
		}

		/// <summary>
		/// Equality operator
		/// </summary>
		/// <param name="v1"></param>
		/// <param name="v2"></param>
		/// <returns></returns>
		public static bool operator ==(SqlWhereBuilderCondition v1, SqlWhereBuilderCondition v2)
		{
			if ( (object.ReferenceEquals(v1, null))
				&& (object.ReferenceEquals(v2, null)) )
				return true;
			else if ( (object.ReferenceEquals(v1, null)) )
				return false;
			else return v1.Equals(v2);
		}

		/// <summary>
		/// Inequality operator
		/// </summary>
		/// <param name="v1"></param>
		/// <param name="v2"></param>
		/// <returns></returns>
		public static bool operator !=(SqlWhereBuilderCondition v1, SqlWhereBuilderCondition v2)
		{
			if ( (object.ReferenceEquals(v1, null))
				&& (object.ReferenceEquals(v2, null)) )
				return false;
			else if ( (object.ReferenceEquals(v1, null)) )
				return true;
			else return !(v1.Equals(v2));
		}



	}
}
